/***********************************************
 * Title:       SystemConfigUtil.java
 * Description: SystemConfigUtil.java
 * Author:      Niyuzhe
 * Create Date: 2010-1-9
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.runtimeConfig;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.younker.waf.db.IBatisConfig;

/**
 * @author Niyuzhe
 *
 */
public class RuntimeConfigUtil
{
        private RuntimeConfigUtil(){}
        
        static
        {                
                _instance = new RuntimeConfigUtil();                
        }
        
        private static RuntimeConfigUtil _instance;
        
        private Map<String, String> map = new HashMap<String, String>(); 
        
        private Logger log = Logger.getLogger(this.getClass());
        
        public static RuntimeConfigUtil getInstance()
        {
                return _instance;
        }
        
        public String getValue( String key) throws NoSuchConfigInfoException
        {

                String value = this.map.get(key);
                if( value == null )
                {

                        value = getValueFromDB(key);
                        this.map.put(key, value);
                }

                return value;
        }
        
        public void setValue( String key, String value )
        {
                saveValue( key, value );
                this.map.put(key, value);
                
                
        }
        
        private String getValueFromDB( String key ) throws NoSuchConfigInfoException
        {                
                String value = "error";
                try
                {
                        value = (String) IBatisConfig.getSqlMapInstance().queryForObject("getValue", key);
                } catch (SQLException e)
                {
                        this.log.fatal("get runtime config Value error[key=" + key + "].", e);
                } 
                if( value == null )
                {
                        log.error("get value return null[key=" + key + "].");
                        throw new NoSuchConfigInfoException(key);
                }
                return value;
        }
        
        private void saveValue( String key, String value ) 
        {
                Map<String, String> map = new HashMap<String, String>();
                map.put("value", value);
                map.put("key", key);
                
                
                try
                {
                        if( this.map.get(key) == null)
                                IBatisConfig.getSqlMapInstance().insert("insertValue", map);
                        else
                                IBatisConfig.getSqlMapInstance().update("saveValue", map);
                } catch (SQLException e)
                {
                        this.log.fatal("Save runtime config infomation error.[key=" + key + ",value=" + value+ "].", e);                       
                }
        }

}
